package com.yto.hrreport.event.buttons {
	import com.yto.hrreport.command.ReportCommand;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class QueryButtonEvent extends ButtonEvent {
		
		private static var parentName : String;
		
		private var param : Object;
		
		private var titleWindow : TitleWindow;
		
		private static var queryEvent : QueryButtonEvent = new QueryButtonEvent();
		
		public function QueryButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : QueryButtonEvent {
			QueryButtonEvent.parentName = parentName;
			return queryEvent
		}
		
		public function queryHandler(event : MouseEvent) : void {
			
			param = ReportCommand.getInstance(parentName).initParam("onBoQuery");
			
			titleWindow = new TitleWindow();
			titleWindow.title = "查询"; 
			titleWindow.name = parentName + "_QueryWindow";
			titleWindow.width = 300;    
			titleWindow.height = 150;    
			titleWindow.addEventListener(CloseEvent.CLOSE, closeDialog);    
			
			var lblBillType : Label = new Label();
			lblBillType.name = parentName + "_lblQueryBillType";
			lblBillType.text = "报表类型";
			lblBillType.left = 20;
			lblBillType.top = 25;
			
			var txtBillType : TextInput = new TextInput();
			txtBillType.name = parentName + "_txtQueryBillType";
			txtBillType.enabled = false;
			txtBillType.text = parentName;
			txtBillType.left = 85;
			txtBillType.top = 20;
			
			var lblPeriod : Label = new Label();
			lblPeriod.name = parentName + "_lblQueryPeriod";
			lblPeriod.text = "统计期间";
			lblPeriod.left = 20;
			lblPeriod.top = 55;
			
			var txtPeriod : TextInput = new TextInput();
			txtPeriod.name = parentName + "_txtQueryPeriod";
			txtPeriod.left = 85;
			txtPeriod.top = 50;
			
			var btnEnter : Button = new Button();
			btnEnter.name = parentName + "_btnEnter";
			btnEnter.label = "确定";
			btnEnter.width = btnWidth;
			btnEnter.left = 50;
			btnEnter.bottom = 10;
			btnEnter.addEventListener(MouseEvent.CLICK , onBtnEnter);
			
			
			var btnCancle : Button = new Button();
			btnCancle.name = parentName + "_btnCancle";
			btnCancle.label = "取消";
			btnCancle.width = btnWidth;
			btnCancle.right = 50;
			btnCancle.bottom = 10;
			btnCancle.addEventListener(MouseEvent.CLICK , onBtnCancle);
			
			var canvas : Canvas = new Canvas();
			canvas.name = parentName + "_queryCard";
			canvas.percentHeight = 100;
			canvas.percentWidth = 100;
			
			canvas.addChild(lblBillType);
			canvas.addChild(txtBillType);
			canvas.addChild(lblPeriod);
			canvas.addChild(txtPeriod);
			canvas.addChild(btnEnter);
			canvas.addChild(btnCancle);
			
			titleWindow.addElement(canvas);
			
			PopUpManager.addPopUp(titleWindow, hrreport.ui, true);    
			PopUpManager.centerPopUp(titleWindow);
		}
		
		public function onBtnEnter(event : MouseEvent) : void {
			
			PopUpManager.removePopUp(titleWindow); 
			PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
			PopUpManager.centerPopUp(hrreport.waitWindow);
			
			ReportCommand.getInstance(parentName).setTitleWindow(titleWindow);
			param = ReportCommand.getInstance(parentName).initParam("onBoEntry");
			
			var tableServer : RemoteObject = new RemoteObject;
			tableServer.destination = IProcessor.TableProcessor;
			tableServer.queryHeadData(parentName , param.periodByQuery);
			tableServer.addEventListener(ResultEvent.RESULT , ReportCommand.getInstanceByTitleWindow(parentName , titleWindow).queryHeadData);
		}
		
		public function onBtnCancle(event : MouseEvent) : void {
			PopUpManager.removePopUp(titleWindow);
		}
		
		public function closeDialog(event : CloseEvent) : void {
			PopUpManager.removePopUp(titleWindow);
		}
	}
}